/**
 * Copyright &copy; 2012-2013 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 */
package com.oplink.webbase.modules.sys.utils;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.oplink.webbase.common.utils.CacheUtils;
import com.oplink.webbase.common.utils.SpringContextHolder;
import com.oplink.webbase.common.utils.UserUtils;
import com.oplink.webbase.modules.sys.dao.DictDao;
import com.oplink.webbase.modules.sys.entity.Dict;

/**
 * 字典工具类
 * 
 * @author ThinkGem
 * @version 2013-5-29
 */
public class DictUtils {

	private static DictDao dictDao = SpringContextHolder.getBean(DictDao.class);

	public static final String CACHE_DICT_MAP = "dictMap";
	public static final String CACHE_DICT_MAP_TYPE = "dictMapType";

	public static String getDictLabel(String value, String type, String defaultValue) {
		if (StringUtils.isNotBlank(type) && StringUtils.isNotBlank(value)) {
			for (Dict dict : getDictList(type)) {
				if (type.equals(dict.getType()) && value.equals(dict.getValue())) {
					return dict.getLabel();
				}
			}
		}
		return defaultValue;
	}

	public static String getDictValue(String label, String type, String defaultLabel) {
		if (StringUtils.isNotBlank(type) && StringUtils.isNotBlank(label)) {
			for (Dict dict : getDictList(type)) {
				if (type.equals(dict.getType()) && label.equals(dict.getLabel())) {
					return dict.getValue();
				}
			}
		}
		return defaultLabel;
	}

	/**
	 * 获取所有的字典类型
	 * 
	 * @return
	 */
	public static List<Dict> getAllDictType() {
		@SuppressWarnings("unchecked")
		// List<Dict> listDictType = (List<Dict>) CacheUtils.get(CACHE_DICT_MAP_TYPE);
		// if (listDictType == null) {
		List<Dict> listDictType = dictDao.findAllListByType(UserUtils.getCurrentUserId());
		/*
		 * CacheUtils.put(CACHE_DICT_MAP_TYPE, listDictType);
		 * } else {
		 * listDictType = (List<Dict>) CacheUtils.get(CACHE_DICT_MAP_TYPE);
		 * }
		 */
		return listDictType;
	}

	public static List<String> getDictJsonList(String type) {
		List<String> outList = new ArrayList<String>();
		List<Dict> listDict = getDictList(type);
		Iterator<Dict> iter = listDict.iterator();
		while (iter.hasNext()) {
			Dict dict = iter.next();
			outList.add(JSONObject.toJSONString(dict));
		}
		return outList;
	}

	public static List<Dict> getDictList(String type) {
		@SuppressWarnings("unchecked")
		Map<String, List<Dict>> dictMap = (Map<String, List<Dict>>) CacheUtils.get(CACHE_DICT_MAP);
		if (dictMap == null) {
			dictMap = Maps.newHashMap();
			List<Dict> listDict = dictDao.findAllListById(UserUtils.getCurrentUserId());
			for (Dict dict : listDict) {
				List<Dict> dictList = dictMap.get(dict.getType());
				if (dictList != null) {
					dictList.add(dict);
				} else {
					dictMap.put(dict.getType(), Lists.newArrayList(dict));
				}
			}
			CacheUtils.put(CACHE_DICT_MAP, dictMap);
		}
		List<Dict> dictList = Lists.newArrayList();
		if (dictList != null) {
			dictList = dictMap.get(type);
		}
		return dictList;
	}

}
